package com.zxx.xin.shorthand.helper;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xin on 2018/3/22.
 */

public class PermissionHelper {

    private Activity mContext;
    public static int requestCode = 1001;

    public PermissionHelper(Activity context){
        mContext = context;
    }

    private boolean checkPermission(String permission){
        if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED){
            return false;
        }
        return true;
    }

    public void requestPermissions(String[] permissions){
        List<String> permissionsNeedRequest = new ArrayList<>();
        for (String permission : permissions){
            if (!checkPermission(permission)){
                permissionsNeedRequest.add(permission);
            }
        }
        if (permissionsNeedRequest.size() > 0){
            String[] permissionArray = new String[permissionsNeedRequest.size()];
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
                ActivityCompat.requestPermissions(mContext, permissionsNeedRequest.toArray(permissionArray), requestCode);
            }
        }
    }
}
